<?php
/*
 * @Author: your name
 * @Date: 2020-08-28 16:29:34
 * @LastEditTime: 2020-11-23 15:35:53
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /v3/application/adminapiv3/controller/SystemConfig.php
 */

namespace app\adminapiv3\controller;


class SystemConfig extends Base
{
    /**
     * @name: 获取配置
     * @Param:
     * @Description:
     * @Author: foo
     * @Date: 2019-09-20 16:28:09
     */
    function configGet ()
    {
        $server = new \app\common\service\SystemConfig();
        $data = $server->configGet();

        $this->jsonOutPut($data);
    }

    /**
     * @name: 客户端更新配置
     * @Param:
     * @Description:
     * @Author: foo
     * @Date: 2019-09-20 16:28:20
     */
    function configModify ()
    {
        global $logContent;
        $logContent = '修改系统设置';

        $data['id'] = $this->filterInput('id*', 'post');//配置类型

        $data['mode'] = $this->filterInput('mode*', 'post', 1);//设备模式 1单机模式 2双机热备模式
        $data['process'] = $this->filterInput('process*', 'post', 'drop');//防火拒绝默认处理方式 drop 仅拒绝 reject拒绝并返回错误页面
        $data['main_address'] = $this->filterInput('main_address', 'post', '');//主机地址
        $data['slave_address'] = $this->filterInput('slave_address', 'post', '');//从机地址
        $data['cpu'] = $this->filterInput('cpu', 'post', '');//cpu
        $data['memory'] = $this->filterInput('memory', 'post', '');//内存
        $data['duration'] = $this->filterInput('duration', 'post', '');//持续时间
        $data['ip'] = $this->filterInput('ip', 'post', '');//管理口配置ip地址
        $data['mask'] = $this->filterInput('mask', 'post', '');//管理口配置子网掩码
        $data['gateway'] = $this->filterInput('gateway', 'post', '');//管理口配置默认网关
        $data['dns'] = $this->filterInput('dns', 'post', '');//管理口配置默认网关

        $data['is_report_remote'] = $this->filterInput('is_report_remote', 'post');//是否允许上报日志到云端
        $data['platform_ip'] = $this->filterInput('platform_ip', 'post');//二级平台ip地址

        $server = new \app\common\service\SystemConfig();
        $server->configModify($data);

        $dataInfo = db('system_config')->where('id', $data['id'])->find();

        if(!empty($dataInfo) && $dataInfo['type'] == 2){//硬件版才生成配置文件

            //修改代理ip
            db('server_proxy')->where('ip', '<>', '127.0.0.1')->update(['ip' => $data['ip']]);

            file_put_contents('./config/system_config.conf', json_encode($data));
            
            global $online_host;
            $online_host = db('host')->field('id')->where('is_software', 0)->select();
            
            $this->Async('Wipf', 'Config');
        }


        $this->jsonOutPut();
    }
}